package Proj_pack;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

// Diese Klasse wurde anhand eines Bsp. implementiert/umgeschrieben (Quelle: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/)
public class xml2graph_parser {
	private SAXParserFactory factory;
	//	private SAXParser saxParser;
	private DefaultHandler handler;
	//	private static CountDownLatch latch;

	private static Graph graph;

	public xml2graph_parser(Graph gr) {
		try {
			factory	= SAXParserFactory.newInstance();
			//			saxParser = factory.newSAXParser();
			graph= gr;
			//			DefaultHandler[] defHan = new DefaultHandler();
			handler = new DefaultHandler() {

				int aktId=0;
				int aktSrcid=0;
				boolean bId = false;
				boolean bCap = false;
				boolean bSrcid = false;
				boolean bDstid = false;
				private Graph gr = graph;

				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
				throws SAXException {
					if (qName.equalsIgnoreCase("edge")){
						netz2135.edgeWait.countDown();
						boolean b=false; do{ 	b=false;
							try {netz2135.edgeWait.await();}catch(InterruptedException e1){b=true;}
						}while(b);
//						do{
//							try {
//								//							System.out.println("wantLatch: " + Thread.currentThread());
//								netz2135.edgeWait.await();								//wait until all thread have finished nodes
//								//							System.out.println("passedLatch: "+ Thread.currentThread());
//							} catch (InterruptedException e){}
//						}while(netz2135.edgeWait.getCount()< netz2135.many);
					}

					if (qName.equalsIgnoreCase("ID")) {
						bId = true;
					}

					if (qName.equalsIgnoreCase("CAPACITY")) {
						bCap = true;
					}

					if (qName.equalsIgnoreCase("srcid")) {
						bSrcid = true;
					}

					if (qName.equalsIgnoreCase("dstid")) {
						bDstid = true;
					}

				}

				public void endElement(String uri, String localName,
						String qName)
				throws SAXException {}

				public void characters(char ch[], int start, int length)
				throws SAXException {


					if (bId) {
						aktId = Integer.valueOf(new String(ch, start, length));

						bId = false;
					}

					if (bCap) {
						String s = new String(ch, start, length);
						//						System.err.println(s+Integer.valueOf(s));
						int cap = Integer.valueOf(s);
						gr.addKn(new Knoten(aktId, cap));
						bCap = false;
					}

					if (bSrcid) {
						aktSrcid = Integer.valueOf(new String(ch, start, length));

						bSrcid = false;
					}

					if (bDstid) {
						int Dtsid = Integer.valueOf(new String(ch, start, length));
						this.gr.getKn(Dtsid).addPre(gr.getKn(aktSrcid));
						this.gr.getKn(aktSrcid).addNf(gr.getKn(Dtsid));
						bDstid = false;
					}

				}

			};
		}catch (Exception e) {} // should not happen

	}
	public void createGraph(File f){
		//			System.err.println("doFile: " +f);
		try {
			try {
				factory.newSAXParser().parse(f, handler);
				netz2135.edgeWait.countDown(); 							//falls teilgraph keine edges hat, darf immer passieren, da an diesem punkt die anderen entweder auf edges warten oder ebenfalls keine haben
			} catch (SAXException e) {						// should not happen
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}
}
